package com.aind.user.controller;

import com.aind.common.constant.SessionContant;
import com.aind.common.utils.verify.VerifyCodeUtil;
import com.aind.user.model.User;
import com.aind.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by lpeng on 2017/1/20.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static Logger logger = Logger.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @RequestMapping("/save")
    public void save(){
        userService.saveTest();
    }

    /**
     * 更新用户密码
     * @param password 密码
     * @param newpassword   新密码
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/modifyPswd")
    public ModelAndView modifyPswd(@RequestParam(value = "password", required = true) String password,
                                   @RequestParam(value = "newpassword", required = true) String newpassword,
                                   HttpServletRequest request, HttpSession session) {
        ModelAndView mav = new ModelAndView("/jsps/msg");
        User session_user = (User) session.getAttribute(SessionContant.SESSION_USER);
        if (null == session_user){
            mav.addObject("code", "error");
            mav.addObject("msg", "未登录");
            return mav;
        }
        if (!userService.getUserByUserId(session_user.getUserId()).getPassword().equals(password)){
            mav.addObject("code", "error");
            mav.addObject("msg", "原始密码错误");
            return mav;
        }
        if (StringUtils.isEmpty(newpassword)){
            mav.addObject("code", "error");
            mav.addObject("msg", "新密码不能为空");
            return mav;
        }
        boolean flag = userService.modifyUserPswdById(session_user.getUserId(), newpassword);
        if (flag){
            mav.addObject("code", "success");
            mav.addObject("msg", "密码更新成功");
        } else {
            mav.addObject("code", "error");
            mav.addObject("msg", "密码更新失败");
        }
        return mav;
    }

    /**
     * 验证密码是否正确
     *
     * @param password 密码
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkPswd")
    public boolean checkPswd(String password, HttpSession session) {
        boolean flag = false;
        User session_user = (User) session.getAttribute(SessionContant.SESSION_USER);
        if (null != session_user && StringUtils.isNotEmpty(password)) {
            User user = userService.getUserByUserId(session_user.getUserId());
            if (password.equals(user.getPassword())) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 验证验证码是否正确
     *
     * @param verifyCode 验证码
     * @param session    session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/validateVerifyCode", produces = "text/json;charset=UTF-8")
    public boolean validateVerifyCode(@RequestParam(required = true) String verifyCode, HttpSession session) {
        boolean flag = false;
        String session_verifyCode = (String) session.getAttribute("vCode");
        if (StringUtils.isNotEmpty(session_verifyCode) && session_verifyCode.equalsIgnoreCase(verifyCode)) {
            flag = true;
        }
        return flag;
    }

    /**
     * 获取图片验证码
     *
     * @param response
     * @param session
     * @throws Exception
     */
    @RequestMapping("/getVerifyCode")
    public void getVerifyCode(HttpServletResponse response, HttpSession session) throws Exception {
        VerifyCodeUtil.outVerifyCode(response, session);
    }

}
